/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;

/**
 *
 * @author divad
 */
public class GLSettings {

    private float universeColorRed = 0;
    private float universeColorGreen = 0;
    private float universeColorBlue = 0;
    private float universeColorAlpha = 1;
    private boolean clientStateNormalArray = false;
    private boolean clientStateVertexArray = false;
    private boolean clientStateColorArray = false;
    public static final int FUNC_EQUAL = GL11.GL_EQUAL, FUNC_LESS = GL11.GL_LESS,
            FUNC_GREATER = GL11.GL_GREATER, FUNC_GEQUAL = GL11.GL_GEQUAL,
            FUNC_LEQUAL = GL11.GL_LEQUAL, FUNC_NEVER = GL11.GL_NEVER,
            FUNC_ALWAYS = GL11.GL_ALWAYS;
    private int depthFunc = GLSettings.FUNC_LESS;
    private boolean depthTest = true;
    private int alphaFunc = GLSettings.FUNC_ALWAYS;
    private boolean alphaTest = false;
    private float alphaRefValue = 0f;
    public static final int CULL_BACK = GL11.GL_BACK, CULL_FRONT = GL11.GL_FRONT,
            CULL_FRONT_BACK = GL11.GL_FRONT_AND_BACK;
    private int cullFaceMode = GLSettings.CULL_BACK;
    private boolean cullFace = true;
    public static final int HINT_FASTEST = GL11.GL_FASTEST,
            HINT_NICEST = GL11.GL_NICEST, HINT_DONT_CARE = GL11.GL_DONT_CARE;
    private int fogHint = GLSettings.HINT_DONT_CARE;
    private int generateMipmapHint = GLSettings.HINT_DONT_CARE;
    private int smoothLineHint = GLSettings.HINT_DONT_CARE;
    private int smoothPointHint = GLSettings.HINT_DONT_CARE;
    private int smoothPolygonHint = GLSettings.HINT_DONT_CARE;
    private int perspectiveCorrectionHint = GLSettings.HINT_NICEST;
    public static final int BFACTOR_ZERO = GL11.GL_ZERO, BFACTOR_ONE = GL11.GL_ONE,
            BFACTOR_SRC_COLOR = GL11.GL_SRC_COLOR,
            BFACTOR_1MIN_SRC_COLOR = GL11.GL_ONE_MINUS_SRC_COLOR,
            BFACTOR_DST_COLOR = GL11.GL_DST_COLOR,
            BFACTOR_1MIN_DST_COLOR = GL11.GL_ONE_MINUS_DST_COLOR,
            BFACTOR_SRC_ALPHA = GL11.GL_SRC_ALPHA,
            BFACTOR_1MIN_SRC_ALPHA = GL11.GL_ONE_MINUS_SRC_ALPHA,
            BFACTOR_DST_ALPHA = GL11.GL_DST_ALPHA,
            BFACTOR_1MIN_DST_ALPHA = GL11.GL_ONE_MINUS_DST_ALPHA,
            BFACTOR_SRC_ALPHA_SATURATE = GL11.GL_SRC_ALPHA_SATURATE,
            BFACTOR_1MIN_CONST_ALPHA = GL11.GL_ONE_MINUS_CONSTANT_ALPHA,
            BFACTOR_1MIN_CONST_COLOR = GL11.GL_ONE_MINUS_CONSTANT_COLOR;
    private int blendSourceFactor = GLSettings.BFACTOR_ONE;
    private int blendDestinationFactor = GLSettings.BFACTOR_ZERO;
    private int blendConstRed = 0;
    private int blendConstGreen = 0;
    private int blendConstBlue = 0;
    private int blendConstAlpha = 0;
    private boolean blending = false;
    public static final int BEQUATION_ADD = GL14.GL_FUNC_ADD, BEQUATION_SUBTRACT = GL14.GL_FUNC_SUBTRACT, BEQUATION_RSUBTRACT = GL14.GL_FUNC_REVERSE_SUBTRACT, BEQUATION_MIN = GL14.GL_MIN, BEQUATION_MAX = GL14.GL_MAX;
    private int blendEquation = GLSettings.BEQUATION_ADD;
    private boolean smoothPolygon = false;
    private boolean invertedView;

    public GLSettings() {
    }

///////////////////////////gettery////////////////////////////////
    public boolean isClientStateColorArray() {
        return clientStateColorArray;
    }

    public boolean isClientStateNormalArray() {
        return clientStateNormalArray;
    }

    public boolean isClientStateVertexArray() {
        return clientStateVertexArray;
    }

    public float getUniverseColorAlpha() {
        return universeColorAlpha;
    }

    public float getUniverseColorBlue() {
        return universeColorBlue;
    }

    public float getUniverseColorGreen() {
        return universeColorGreen;
    }

    public float getUniverseColorRed() {
        return universeColorRed;
    }

    public int getFogHint() {
        return fogHint;
    }

    public int getMipMapHint() {
        return generateMipmapHint;
    }

    public int getPerspectiveCorrectionHint() {
        return perspectiveCorrectionHint;
    }

    public int getSmoothLineHint() {
        return smoothLineHint;
    }

    public int getSmoothPointHint() {
        return smoothPointHint;
    }

    public int getSmoothPolygonHint() {
        return smoothPolygonHint;
    }

    public boolean isCullFace() {
        return cullFace;
    }

    public int getCullFaceMode() {
        return cullFaceMode;
    }

    public int getDepthFunc() {
        return depthFunc;
    }

    public boolean isDepthTest() {
        return depthTest;
    }

    public int getAlphaFunc() {
        return alphaFunc;
    }

    public float getAlphaRefValue() {
        return alphaRefValue;
    }

    public boolean isAlphaTest() {
        return alphaTest;
    }

    public int getGenerateMipmapHint() {
        return generateMipmapHint;
    }

    public int getBlendDestinationFactor() {
        return blendDestinationFactor;
    }

    public int getBlendSourceFactor() {
        return blendSourceFactor;
    }

    public int getBlendConstAlpha() {
        return blendConstAlpha;
    }

    public int getBlendConstBlue() {
        return blendConstBlue;
    }

    public int getBlendConstGreen() {
        return blendConstGreen;
    }

    public int getBlendConstRed() {
        return blendConstRed;
    }

    public boolean isBlending() {
        return blending;
    }

    public int getBlendEquation() {
        return blendEquation;
    }

    public boolean isSmoothPolygon() {
        return smoothPolygon;
    }

    public boolean isInvertedView() {
        return invertedView;
    }

    ////////////////////////////settery//////////////////////////////
    public void setClientStateColorArray(boolean clientStateColorArray) {
        if (clientStateColorArray != this.clientStateColorArray) {
            if (clientStateColorArray) {
                GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
            } else {
                GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
            }
            this.clientStateColorArray = clientStateColorArray;
        }
    }

    public void setClientStateNormalArray(boolean clientStateNormalArray) {
        if (clientStateNormalArray != this.clientStateNormalArray) {
            if (clientStateNormalArray) {
                GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
            } else {
                GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
            }
            this.clientStateNormalArray = clientStateNormalArray;
        }
    }

    public void setClientStateVertexArray(boolean clientStateVertexArray) {
        if (clientStateVertexArray != this.clientStateVertexArray) {
            if (clientStateVertexArray) {
                GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
            } else {
                GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
            }
            this.clientStateVertexArray = clientStateVertexArray;
        }
    }

    public void setUniverseColorAlpha(float universeColorAlpha) {
        this.universeColorAlpha = universeColorAlpha;
    }

    public void setUniverseColorBlue(float universeColorBlue) {
        this.universeColorBlue = universeColorBlue;
    }

    public void setUniverseColorGreen(float universeColorGreen) {
        this.universeColorGreen = universeColorGreen;
    }

    public void setUniverseColorRed(float universeColorRed) {
        this.universeColorRed = universeColorRed;
    }

    public void setCullFace(boolean cullFace) {
        this.cullFace = cullFace;
    }

    public void setCullFaceMode(int cullFaceMode) {
        this.cullFaceMode = cullFaceMode;
    }

    public void setDepthFunc(int depthFunc) {
        this.depthFunc = depthFunc;
    }

    public void setDepthTest(boolean depthTest) {
        this.depthTest = depthTest;
    }

    public void setFogHint(int fogHint) {
        this.fogHint = fogHint;
    }

    public void setMipMapHint(int mipMapHint) {
        this.generateMipmapHint = mipMapHint;
    }

    public void setPerspectiveCorrectionHint(int perspectiveCorrectionHint) {
        this.perspectiveCorrectionHint = perspectiveCorrectionHint;
    }

    public void setSmoothLineHint(int smoothLineHint) {
        this.smoothLineHint = smoothLineHint;
    }

    public void setSmoothPointHint(int smoothPointHint) {
        this.smoothPointHint = smoothPointHint;
    }

    public void setSmoothPolygonHint(int smoothPolygonHint) {
        this.smoothPolygonHint = smoothPolygonHint;
    }

    public void setAlphaFunc(int alphaFunc) {
        this.alphaFunc = alphaFunc;
    }

    public void setAlphaRefValue(float alphaRefValue) {
        this.alphaRefValue = alphaRefValue;
    }

    public void setAlphaTest(boolean alphaTest) {
        this.alphaTest = alphaTest;
    }

    public void setGenerateMipmapHint(int generateMipmapHint) {
        this.generateMipmapHint = generateMipmapHint;
    }

    public void setBlendDestinationFactor(int blendDestinationFactor) {
        this.blendDestinationFactor = blendDestinationFactor;
    }

    public void setBlendSourceFactor(int blendSourceFactor) {
        this.blendSourceFactor = blendSourceFactor;
    }

    public void setBlendConstAlpha(int blendConstAlpha) {
        this.blendConstAlpha = blendConstAlpha;
    }

    public void setBlendConstBlue(int blendConstBlue) {
        this.blendConstBlue = blendConstBlue;
    }

    public void setBlendConstGreen(int blendConstGreen) {
        this.blendConstGreen = blendConstGreen;
    }

    public void setBlendConstRed(int blendConstRed) {
        this.blendConstRed = blendConstRed;
    }

    public void setBlending(boolean blending) {
        this.blending = blending;
    }

    public void setBlendEquation(int blendEquation) {
        this.blendEquation = blendEquation;
    }

    public void setSmoothPolygon(boolean smoothPolygon) {
        this.smoothPolygon = smoothPolygon;
    }

    public void setInvertedView(boolean invertedView) {
        this.invertedView = invertedView;
    }

    ///////////////////////////////další/////////////////////////////////////
    public void setupGL() {
        GL11.glClearColor(this.universeColorRed, this.universeColorGreen, this.universeColorBlue, this.universeColorAlpha);
        if (this.depthTest) {
            GL11.glEnable(GL11.GL_DEPTH_TEST);
            GL11.glDepthFunc(this.depthFunc);
        } else {
            GL11.glDisable(GL11.GL_DEPTH_TEST);
        }

        if (this.alphaTest) {
            GL11.glEnable(GL11.GL_ALPHA_TEST);
            GL11.glAlphaFunc(this.alphaFunc, this.alphaRefValue);
        } else {
            GL11.glDisable(GL11.GL_ALPHA_TEST);
        }

        if (this.cullFace) {
            GL11.glEnable(GL11.GL_CULL_FACE);
            if (this.invertedView) {
                if (this.cullFaceMode == GLSettings.CULL_BACK) {
                    GL11.glCullFace(GLSettings.CULL_FRONT);
                } else if (this.cullFaceMode == GLSettings.CULL_FRONT) {
                    GL11.glCullFace(GLSettings.CULL_BACK);
                } else {
                    GL11.glCullFace(GLSettings.CULL_FRONT_BACK);
                }
            } else {
                GL11.glCullFace(this.cullFaceMode);
            }
//            GL11.glCullFace(this.cullFaceMode);
        } else {
            GL11.glDisable(GL11.GL_CULL_FACE);
        }

        if (this.blending) {
            GL11.glEnable(GL11.GL_BLEND);
            GL11.glBlendFunc(this.blendSourceFactor, this.blendDestinationFactor);
            GL14.glBlendEquation(this.blendEquation);
            if (this.blendSourceFactor == GLSettings.BFACTOR_1MIN_CONST_ALPHA || this.blendSourceFactor == GLSettings.BFACTOR_1MIN_CONST_COLOR || this.blendDestinationFactor == GLSettings.BFACTOR_1MIN_CONST_ALPHA || this.blendDestinationFactor == GLSettings.BFACTOR_1MIN_CONST_COLOR) {
                GL14.glBlendColor(this.blendConstRed, this.blendConstGreen, this.blendConstBlue, this.blendConstAlpha);
            }
        } else {
            GL11.glDisable(GL11.GL_BLEND);
        }

        if (this.smoothPolygon) {
            GL11.glEnable(GL11.GL_POLYGON_SMOOTH);
        } else {
            GL11.glDisable(GL11.GL_POLYGON_SMOOTH);
        }
        GL11.glHint(GL11.GL_FOG_HINT, this.fogHint);
        GL11.glHint(GL14.GL_GENERATE_MIPMAP_HINT, this.generateMipmapHint);
        GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, this.smoothLineHint);
        GL11.glHint(GL11.GL_POINT_SMOOTH_HINT, this.smoothPointHint);
        GL11.glHint(GL11.GL_POLYGON_SMOOTH_HINT, this.smoothLineHint);
        GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, this.perspectiveCorrectionHint);

        this.setClientStateColorArray(false);
        this.setClientStateNormalArray(false);
        this.setClientStateVertexArray(false);
    }
}
